package org.geekbang.thinking.in.spring.dependency.source;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.Resource;

/**
 * 外部化配置作为依赖来源实例
 *
 */
@Configuration
@PropertySource(value = "classpath:META-INF/default.properties", encoding = "UTF-8")
public class ExternalConfigurationDependencySourceDemo {

    @Value("${user.id:0}")
    private Long id;

    @Value("${user.username}")
    private String username;

    @Value("${user.resource:classpath://default.properties}")
    private Resource resource;

    public static void main(String[] args) {
        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

        // 注册 Configuration Class（配置类） -> Spring Bean
        context.register(ExternalConfigurationDependencySourceDemo.class);

        // 启动 Spring 应用上下文
        context.refresh();

        ExternalConfigurationDependencySourceDemo demo = context.getBean(ExternalConfigurationDependencySourceDemo.class);

        System.out.println("id = " + demo.id);
        System.out.println("username = " + demo.username);
        System.out.println("resource = " + demo.resource);

        // 关闭 Spring 应用上下文
        context.close();

    }

}
